﻿using Localization.Resources.AbpUi;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System.IO;
using Volo.Abp.Account.Web;
using Volo.Abp.Account.Web.Localization;
using Volo.Abp.AspNetCore.Mvc.Localization;
using Volo.Abp.AspNetCore.Mvc.UI.Theme.SB;
using Volo.Abp.Autofac;
using Volo.Abp.AutoMapper;
using Volo.Abp.Emailing;
using Volo.Abp.Extension.Web.Settings;
using Volo.Abp.Identity.Localization;
using Volo.Abp.Identity.Web;
using Volo.Abp.Localization;
using Volo.Abp.Localization.Resources.AbpValidation;
using Volo.Abp.Modularity;
using Volo.Abp.SettingManagement.Localization;
using Volo.Abp.SettingManagement.Web;
using Volo.Abp.SettingManagement.Web.Pages.SettingManagement;
using Volo.Abp.TenantManagement.Localization;
using Volo.Abp.TenantManagement.Web;
using Volo.Abp.VirtualFileSystem;

namespace Volo.Abp.Extension.Web
{
    [DependsOn(
         typeof(AbpEmailingModule),
         typeof(AbpAutofacModule),
         typeof(AbpIdentityWebModule),
         typeof(AbpAccountWebModule),
         typeof(AbpTenantManagementWebModule),
         typeof(AbpSettingManagementWebModule),
         typeof(AbpAspNetCoreMvcUISBThemeModule)
         )]
    public class VoloAbpExtensionWebModule : AbpModule
    {

        public override void PreConfigureServices(ServiceConfigurationContext context)
        {

            //使用DataAnnotations转换label为多语言
            //详情见 项目Volo.Abp.AspNetCore.Mvc中的AbpDataAnnotationAutoLocalizationMetadataDetailsProvider
            context.Services.PreConfigure<AbpMvcDataAnnotationsLocalizationOptions>(options =>
            {
                options.AddAssemblyResource(
                    typeof(IdentityResource),
                    typeof(VoloAbpExtensionWebModule).Assembly
                );
            });
        }


        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var hostingEnvironment = context.Services.GetHostingEnvironment();
            var configuration = context.Services.GetConfiguration();
            var services = context.Services;


            ConfigureAutoMapper();
            ConfigureVirtualFileSystem(hostingEnvironment);
            ConfigureLocalizationServices();
            ConfigureSettingManagementServices();
        }

        private void ConfigureAutoMapper()
        {
            Configure<AbpAutoMapperOptions>(options =>
            {
                options.AddProfile<VoloAbpExtensionWebAutoMapperProfile>();
            });
        }

        private void ConfigureVirtualFileSystem(IHostingEnvironment hostingEnvironment)
        {
            Configure<VirtualFileSystemOptions>(options =>
            {
                options.FileSets.AddEmbedded<VoloAbpExtensionWebModule>("Volo.Abp.Extension.Web");
            });

            if (hostingEnvironment.IsDevelopment())
            {
                Configure<VirtualFileSystemOptions>(options =>
                {
                    options.FileSets.ReplaceEmbeddedByPhysical<VoloAbpExtensionWebModule>(Path.Combine(hostingEnvironment.ContentRootPath, string.Format("..{0}Volo.Abp.Extension.Web", Path.DirectorySeparatorChar)));
                });
            }
        }


        private void ConfigureLocalizationServices()
        {
            Configure<AbpLocalizationOptions>(options =>
            {
                options.Languages.Add(new LanguageInfo("zh-CN", "zh-CN", "简体中文"));
                options.Languages.Add(new LanguageInfo("en", "en", "English"));


                options.Resources.Get<AccountResource>().AddVirtualJson("/Localization/Account");
                options.Resources.Get<AbpUiResource>().AddVirtualJson("/Localization/AbpUi");
                options.Resources.Get<AbpValidationResource>().AddVirtualJson("/Localization/Validation");
                options.Resources.Get<IdentityResource>().AddVirtualJson("/Localization/Identity");
                options.Resources.Get<AbpTenantManagementResource>().AddVirtualJson("/Localization/TenantManagement");
                options.Resources.Get<AbpSettingManagementResource>().AddVirtualJson("/Localization/SettingManagement");
            });
        }

        private void ConfigureSettingManagementServices()
        {

            Configure<SettingManagementPageOptions>(options =>
            {
                options.Contributors.Add(new SettingPageContributor());
            });
        }


        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            var env = context.GetEnvironment();
        }

    }
}
